package com.syqc.videos.real

import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.ActionData
import com.syqc.entity.VideoHost
import com.syqc.monitor.R
import com.syqc.net.ServiceHelper
import com.syqc.utils.VideoHostUtil

/**
 *@author  hfjs
 *@time   2024/12/31 10:33
 *@desc
 */
class ControllerVM: AutoDisposeViewModel() {
    //服务器地址
    private lateinit var videoHost: VideoHost
    var carId: String = ""
        set(value) {
            field = value
            if (value.isNotEmpty()) videoHost = VideoHostUtil.getVideoHost(value)
        }


    //是否28181设备
    var is28181 = false

    //云台控制速度
    var speed = 127

    //控制通道
    var ctrlChannel = ""
    /** drct：1上移 2下移 3左移 4右移 0停止 speed：旋转速度0-255,默认127 */
    private fun getControlUrl(drct: Int): String {
        return "${VideoHostUtil.getHost(videoHost)}/phone.cloud_ctr.0.$carId.$ctrlChannel.$drct.$speed"
    }

    /**
     *1.调整焦距(0焦距变小 1焦距变大) 2.调整光圈(0光圈放大 1光圈缩小) 3.调整雨刷(0雨刷关闭 1雨刷打开)
     *4.调整补光(0补光关闭 1补光打开) 5.调整变倍(0变倍调大 1变倍调小)
     * @param type
     * @param action
     * @return
     */
    private fun getActionUrl(type: Int, action: Int): String {
        return "${VideoHostUtil.getHost(videoHost)}/phone.cloud_ctr.$type.$carId.$ctrlChannel.$action"
    }

    /**
     * 判断参数是否合格
     *
     * @return
     */
    fun isParamsOk(): Boolean {
        if (carId.isEmpty()) {
            toast(R.string.hint_toast_choose_car)
            return true
        }
        if (ctrlChannel.isEmpty()) {
            toast(R.string.hint_toast_choose_channel)
            return true
        }
        return false
    }

    //云台控制
    fun control(drct: Int) {
        if (isParamsOk()) return
        toastLoading(true)
        ServiceHelper.getRepository().getVideoState(getControlUrl(drct)).autoDispose(this)
            .subscribe({
                if (it == "1") {
                    toastLoading(false)
//                toast(R.string.toast_set_success)
                    return@subscribe
                }
            }, error)
    }

    /**
     * 1.调整焦距(0焦距变小 1焦距变大) 2.调整光圈(0光圈放大 1光圈缩小) 3.调整雨刷(0雨刷关闭 1雨刷打开)
     * 4.调整补光(0补光关闭 1补光打开) 5.调整变倍(0变倍调大 1变倍调小)
     *
     * @param type
     * @param action
     */
    fun changeAction(type: Int, action: Int) { 
        if (isParamsOk()) return
        toastLoading(true)
        ServiceHelper.getRepository().getVideoState(getActionUrl(type, action)).autoDispose(this)
            .subscribe({
                if (it == "1") {
                    if (type == 3) setWiper(action)
                    else if (type == 4) setLight(action)
                    toastLoading(false)
                    return@subscribe
                }
            }, error)
    }

    private fun setLight(light: Int) {
        val action = getAction()
        putAction(light, action.wiper)
    }

    private fun setWiper(wiper: Int) {
        val action = getAction()
        putAction(action.light, wiper)
    }


    private val actionMap = HashMap<String, ActionData>()

    private fun putAction(light: Int, wiper: Int) {
        val actionData = actionMap[ctrlChannel]
        if (actionData == null) {
            actionMap[ctrlChannel] = ActionData(light, wiper)
        } else {
            actionData.light = light
            actionData.wiper = wiper
        }
        actionLive.postValue(true)
    }

    fun getAction(): ActionData {
        return actionMap[ctrlChannel] ?: ActionData()
    }

    var actionLive = MutableLiveData<Boolean>()

    fun clearMap() {
        actionMap.clear()
    }

}